package com.example.newsapplication.activity;

import android.widget.Button;
import android.widget.EditText;

import com.example.newsapplication.R;
import com.example.newsapplication.api.Api;
import com.example.newsapplication.api.ApiCallback;
import com.example.newsapplication.api.ApiConfig;
import com.example.newsapplication.util.StringUtils;

import java.util.HashMap;

public class RegisterActivity extends BaseActivity {
    private Button btn_Register;
    private EditText et_account;
    private EditText et_pwd;

    @Override
    protected int initLayout() {
        return R.layout.activity_register;
    }

    @Override
    protected void initView() {
        btn_Register = findViewById(R.id.btn_register);
        et_account = findViewById(R.id.et_account);
        et_pwd = findViewById(R.id.et_pwd);
    }

    @Override
    protected void initData() {
        btn_Register.setOnClickListener(v -> {
            String account = et_account.getText().toString();
            String pwd = et_pwd.getText().toString();
            // TODO: 登录逻辑
            register(account,pwd);
        });
    }

    private void register(String account, String pwd) {
        if (StringUtils.isEmpty(account)){
            showToast("请输入账号");
            return;
        }
        if (StringUtils.isEmpty(pwd)){
            showToast("请输入密码");
            return;
        }
        // TODO: 注册成功跳转
        HashMap<String, Object> params = new HashMap<String, Object>();
        params.put("mobile", account);
        params.put("password", pwd);
        Api.config(ApiConfig.REGISTER, params).post(this, new ApiCallback() {
            @Override
            public void onSuccess(String result) {
                showToastAsync(result);
            }

            @Override
            public void onFailure(Exception e) {
                showToastAsync(e.getMessage());
            }
        });

    }
}